package com.healthcare.client.service.impl;

import com.healthcare.client.form.ApplyReferralForm;
import com.healthcare.client.mapper.GroupMapper;
import com.healthcare.client.mapper.ReferralMapper;
import com.healthcare.client.pojo.Group;
import com.healthcare.client.pojo.Patient;
import com.healthcare.client.mapper.PatientMapper;
import com.healthcare.client.pojo.Referral;
import com.healthcare.client.service.PatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.healthcare.client.vo.PatientEditVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 患者表 服务实现类
 * </p>
 *
 * @author 新华医院转诊联盟
 * @since 2020-04-13
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private ReferralMapper referralMapper;

    @Autowired
    private GroupMapper groupMapper;

    /**
     *  查询患者详细信息
     * @param examineId 会诊或转诊id
     * @param patientId 患者id
     * @param type  类型，用来区分是会诊还是转诊 0- 会诊 1 转诊
     * @return
     */
    @Override
    public PatientEditVo getPatientEditVo(Integer examineId, Integer patientId, Integer type) {
        // 返回的数据模型
        PatientEditVo patientEditVo = null;
        // 当type == 0 时则为会诊，调用会诊的查询方法
        if(type == 0){
            patientEditVo = baseMapper.getGroupPatientEditVo(examineId,patientId);
        }
        // 当type == 1 时则为转诊，调用转诊的查询方法
        if(type == 1){
            patientEditVo = baseMapper.getReferralPatientEditVo(examineId,patientId);
        }
        return patientEditVo;
    }

    /**
     * 更新患者详细信息
     *
     * @param type              类型，用来区分是会诊还是转诊 0- 会诊 1 转诊
     * @param applyReferralForm 转诊表单对象
     */
    @Override
    public void updatePatientInfo(Integer type, ApplyReferralForm applyReferralForm) {

        Patient patient  = new Patient();
        BeanUtils.copyProperties(applyReferralForm,patient);
        System.out.println(patient);
        baseMapper.updateById(patient);

        if (type == 0){
            Group group= new Group();
            BeanUtils.copyProperties(applyReferralForm,group);
            System.out.println(group);
            groupMapper.updateById(group);
        }
        if (type == 1){
            Referral referral = new Referral();
            BeanUtils.copyProperties(applyReferralForm,referral);
            System.out.println(referral);
            referralMapper.updateById(referral);
        }
    }
}
